package com.totcy.atom.view.activity;

import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.TextView;

import com.totcy.atom.R;
import com.totcy.atom.adapter.PictureBrowsingAdapter;
import com.totcy.atom.dialog.ShareDialog;
import com.totcy.atom.utils.SaveImageUtils;
import com.totcy.atom.utils.WeChatShareUtils;
import com.totcy.baselibrary.utils.SPUtil;
import com.totcy.baselibrary.utils.StatusBarUtil;
import com.totcy.baselibrary.utils.ToastUtil;
import com.totcy.baselibrary.widget.HackyViewPager;
import com.totcy.baselibrary.widget.guideview.Component;
import com.totcy.baselibrary.widget.guideview.Guide;
import com.totcy.baselibrary.widget.guideview.GuideBuilder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * description: 大图浏览
 *
 * @author: Totcy
 * date: 2017/11/4 20:03
 * update: 2017/11/4
 */
public class PictureBrowsingActivity extends BaseFullActivity implements ViewPager.OnPageChangeListener, Toolbar.OnMenuItemClickListener, View.OnClickListener {

    private static final String TAG = "PictureBrowsingActivity";
    private static final String FIRST_GUIDE_SAVE_IMG = "first_save_img";
    @BindView(R.id.hvp_images)
    HackyViewPager viewPager;
    private List<String> datas = new ArrayList<>();
    private String currentImgUrl = "";

    /**
     * @return 获取标题名
     */
    @Override
    public String getTitleName() {
        return "";
    }

    /**
     * 布局Id 解析添加到 layout/layout_full_base_activity.xml中
     *
     * @return 子activity layout id
     */
    @Override
    public int getLayoutId() {
        return R.layout.activity_picture_browsing;
    }

    /**
     * 初始化View
     */
    @Override
    public void initView() {
        getToolbarTitleLayout().setBackgroundColor(StatusBarUtil.mixtureColor(0x37bfe9, 0.2f));

        //初始化图片数据
        datas.clear();
        datas = getIntent().getStringArrayListExtra("urlList");
        viewPager.setAdapter(new PictureBrowsingAdapter(this, datas, this));
        int position = getIntent().getIntExtra("position", 0);
        viewPager.setCurrentItem(position);
        //当前选中的地址
        currentImgUrl = datas.get(position);
        viewPager.addOnPageChangeListener(this);
        //图片数量显示
        setTitle(position + 1 + "/" + datas.size());
        //设置右上角菜单
        getToolbarTitleLayout().inflateMenu(R.menu.menu_picture_browsing);
        getToolbarTitleLayout().setOnMenuItemClickListener(this);

        viewPager.post(new Runnable() {
            @Override
            public void run() {
                showGuideView();
            }
        });

    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {

            case R.id.share_image:
                shareImage();
                break;
            case R.id.save_image:
                SaveImageUtils.saveImage(this, datas.get(viewPager.getCurrentItem()));
                break;
        }
        return true;
    }

    /**
     * 图片分享
     */
    private void shareImage() {
        if (TextUtils.isEmpty(currentImgUrl)) {
            ToastUtil.showToast(this, getString(R.string.image_url_is_null));
            return;
        }
        new ShareDialog(this).setOnItemClick(new ShareDialog.OnShareTypeClick() {
            @Override
            public void onItemType(int type) {
                WeChatShareUtils weChatShareUtils = new WeChatShareUtils(PictureBrowsingActivity.this);
                weChatShareUtils.shareImage(
                        currentImgUrl,
                        type);
            }
        }).showDialog();


    }

    @Override
    public boolean isChildLayoutBelowTitle() {
        return false;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        //图片数量显示
        setTitle(position + 1 + "/" + viewPager.getAdapter().getCount());
        currentImgUrl = datas.get(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View v) {
        //切换显示toolbar
        v.setSelected(!v.isSelected());
        switchAnimation(v.isSelected());
    }

    /**
     * 标题栏上移动画
     *
     * @param isShow
     */
    private void switchAnimation(boolean isShow) {
        View toolbar = getToolbarTitleLayout();
        toolbar.clearAnimation();
        toolbar.animate()
                .translationY(isShow ? -toolbar.getHeight() : 0)
                .setInterpolator(new DecelerateInterpolator())
                .setDuration(400)
                .start();
    }

    /**
     * 创建引导层
     */
    private void showGuideView() {
        boolean firstLook = (boolean) SPUtil.get(this, FIRST_GUIDE_SAVE_IMG, true);
        if (!firstLook) {
            return;
        }
        Guide guide = new GuideBuilder()
                .setTargetView(getToolbarTitleLayout())
                .setAlpha(150)
                //.setHighTargetCorner(10)
                //.setHighTargetPadding(5)
                .setOverlayTarget(false)
                .setOutsideTouchable(false)
                .setOnVisibilityChangedListener(new GuideBuilder.OnVisibilityChangedListener() {
                    @Override
                    public void onShown() {
                    }

                    @Override
                    public void onDismiss() {
                        SPUtil.put(PictureBrowsingActivity.this, FIRST_GUIDE_SAVE_IMG, false);
                    }
                })
                .addComponent(new SimpleComponent())
                .createGuide();
        guide.setShouldCheckLocInWindow(false);
        guide.show(this);

    }

    private class SimpleComponent implements Component {

        @Override
        public View getView(LayoutInflater inflater) {
            View view = inflater.inflate(R.layout.guide_layout_right, null);
            TextView textView = view.findViewById(R.id.tv_guideMsg);
            textView.setText(R.string.click_save_picture);
            return view;
        }

        @Override
        public int getAnchor() {
            return Component.ANCHOR_BOTTOM;
        }

        @Override
        public int getFitPosition() {
            return Component.FIT_END;
        }

        @Override
        public int getXOffset() {
            return -5;
        }

        @Override
        public int getYOffset() {
            return 10;
        }
    }
}
